package com.tengju.settlement.infrastructure.repository.convert;

import com.tengju.data.domain.shared.CastUtil;
import com.tengju.settlement.domain.model.account.Account;
import com.tengju.settlement.domain.model.account.AccountId;
import com.tengju.settlement.domain.model.account.AccountType;
import com.tengju.settlement.domain.model.account.UserIdCode;
import com.tengju.settlement.domain.shared.Amount;
import com.tengju.settlement.domain.shared.IdObject;
import com.tengju.settlement.infrastructure.persistence.mybatis.dataobject.AccountDO;

import java.util.Optional;

public class AccountConvert {

    public static AccountDO convert(Account account) {
        if (account == null){
            return null;
        }
        AccountDO accountDO = new AccountDO();
        accountDO.setId(Optional.ofNullable(account.getAccountId()).map(IdObject::getValue).map(CastUtil::castInt).orElse(null));
        accountDO.setIdCode((int) account.getIdCode().getValue());
        accountDO.setBalance(account.getBalance().getValue());
//        accountDO.setIsView();
        accountDO.setVersion(account.getVersion());
        accountDO.setFreezeBalance(account.getFreezeBalance().getValue());
        return accountDO;
    }

    public static Account convert(AccountDO accountDO) {
        if (accountDO == null){
            return null;
        }
        return new Account(new AccountId(accountDO.getId()),
                new UserIdCode(accountDO.getIdCode()),
                AccountType.DATA_TREE,
                new Amount(accountDO.getBalance()),
                new Amount(accountDO.getFreezeBalance()),
                "",accountDO.getVersion());
    }
}
